#!/usr/bin/perl

use strict;
use lib '../trunk';
use WWW::Metaweb;

# Used to view a Perl object response, also the most useful module - ever.
use Data::Dumper;

=head1 NAME

metaweb-example.pl - Shows how to use the WWW::Metaweb module

=head1 SYNOPSIS

perl metaweb-example.pl username password

=head1 DESCRIPTION

This Perl script gives a simple example of how to use the WWW::Metaweb module. I will hopefully be extending it to contain more complex examples when I have the time.

If you've got any questions about stuff here, please feel free to email me:
hds (at) caffeineconcepts.com

=cut

my $mh = WWW::Metaweb->connect( server => 'www.freebase.com',
                                auth_uri => '/api/account/login',
                                read_uri => '/api/service/mqlread',
				write_uri => '/api/service/mqlwrite',
				trans_uri => '/api/trans',
                                pretty_json => 1 );

my ($query, $res);

$query = qq({
	"name":"Nico Minoru",
	"type":[],
	"guid":null
});
$mh->add_query('read', $query);
$mh->send_envelope('read');
die $WWW::Metaweb::errstr unless ($mh->result_is_ok());
$res = $mh->result(undef, 'perl');
print Dumper $res;

$mh->clear_queries('read');
my @types = @{$res->{type}};
my $guid = $res->{guid};
my @queries;

for (my $i = 0; $i < @types; $i++)  {
	my $type = $types[$i];

	$types[$i] =~ s/^\W+//;
	$types[$i] =~ s/\W+/_/g;
	$query = {
		guid => $guid,
		type => $type,
		'*' => undef
	};
	
	push @queries, $query;
}

foreach ($mh->read(@queries))  {
	print Dumper $_;
}

exit;

